/*
 * 课程
 */
import Course from 'constants/course';
import { CALL_BOOK_API } from '../middlewares';

/**
 * 创建Course课程
 * 不需要学期ID了，Course已经上最顶层
 * @param data CourseModal
 */
export function createCourse(data: Record<string, any>) {
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Course.CREATE, Course.CREATE_SUCCESS, Course.CREATE_FAIL],
      uri: `/course-api/a/courses`,
      method: 'POST',
      body: data,
    },
  };
}

/**
 * 更新课程
 * 不需要学期ID了，Course已经上最顶层
 * @param courseId 课程ID
 * @param data CourseModal
 */
export function updateCourse(courseId: number, data: any) {
  return {
    courseId,
    [CALL_BOOK_API]: {
      types: [Course.UPDATE, Course.UPDATE_SUCCESS, Course.UPDATE_FAIL],
      uri: `/course-api/a/courses/${courseId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * 查询课程列表
 * 已经没有学期的概念，(API已没做过滤)传递一个数字即可
 */
export function fetchCourseList() {
  return {
    type: 'fetchCourses',
    [CALL_BOOK_API]: {
      types: [Course.FETCH_LIST, Course.FETCH_LIST_SUCCESS, Course.FETCH_LIST_FAIL],
      uri: `/course-api/a/courses`,
      method: 'GET',
    },
  };
}

/**
 * 查询课程ID的所有课时
 * @param courseId 课程ID
 */
export function fetchCourseLessons(courseId: number) {
  return {
    courseId,
    [CALL_BOOK_API]: {
      types: [
        Course.FETCH_LESSON_LIST,
        Course.FETCH_LESSON_LIST_SUCCESS,
        Course.FETCH_LESSON_LIST_FAIL,
      ],
      uri: `/course-api/a/courses/${courseId}/lessons`,
      method: 'GET',
    },
  };
}

/**
 * 课时排序
 * @param courseId
 * @param ids
 */
export function sortCourseLessons(courseId: number, lessonIds: number[]) {
  return {
    courseId,
    [CALL_BOOK_API]: {
      types: [Course.SORT_LESSON, Course.SORT_LESSON_SUCCESS, Course.SORT_LESSON_FAIL],
      uri: `/course-api/a/courses/${courseId}/lessons-sequence`,
      method: 'PUT',
      body: {
        lessonIds,
      },
    },
  };
}

/**
 * 更新课程的发布状态
 * @param courseId 课程ID
 * @param isPublished 状态: 0: 未发布，1: 已发布
 */
export function updateCoursePublish(courseId: number, isPublished: number) {
  return {
    courseId,
    [CALL_BOOK_API]: {
      types: [
        Course.UPDATE_PUBLISH_STATUS,
        Course.UPDATE_PUBLISH_STATUS_SUCCESS,
        Course.UPDATE_PUBLISH_STATUS_FAIL,
      ],
      uri: `/course-api/a/courses/${courseId}/publish`,
      method: 'PUT',
      body: {
        isPublished,
      },
    },
  };
}
